package com.lin.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.reflect.Proxy;
import java.text.SimpleDateFormat;

public class TimeLogBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return Proxy.newProxyInstance(
                bean.getClass().getClassLoader(),
                bean.getClass().getInterfaces(),
                ((proxy, method, args) -> {
                    System.out.println("method: " + method.getName() +
                            ", 执行时间: " + simpleDateFormat.format(System.currentTimeMillis()));
                    Object invoke = method.invoke(bean, args);
                    System.out.println("method: " + method.getName() +
                            ", 执行时间: " + simpleDateFormat.format(System.currentTimeMillis()));
                    return invoke;
                })
        );
    }
}
